#!usr/bin/env python
# -*- coding:utf-8 -*-
# user nanme: Mr.zhang
# data time : 2022/11/22   3:03 下午
# project   : uninstall.py
# IDE       : PyCharm
# email     :17600960626@163.com
import logging
import subprocess
import sys

from driver.conf_devices import devices



class Auto_uninstall(devices):


    # apk 字典
    def apk_dictionary(self,s1):
        self.apk_name = []
        apk = {
            "ikey": "com.emoji.ikeyboard",
            "ikeyd": "com.emoji.ikeyboard.debug",
            "gb": "com.google.android.inputmethod.latin",
            "swk": "com.touchtype.swiftkey",
            "touchpal": "com.cootek.smartinputv5",
            "facemoji": "com.simejikeyboard",
            "indic": "com.kikatech.keyboard.emoji.indian.debug",
            "oem": "com.kikaoem.hw.qisiemoji.inputmethod",
            "kika": "com.qisiemoji.inputmethod.debug",
            "huawei": "com.hw.inputmethod",
            "ziyan": "com.huawei.ohos.inputmethod",
            "pro": "com.emoji.coolkeyboard",
            "prod": "com.emoji.coolkeyboard.debug",
            "prot": "kika.emoji.keyboard.teclados.clavier.debug",
            "xp": "com.kikatech.hackkeyboardxposedmodule",
            "prohw": "com.emoji.coolkeyboard.hw.store",
            "vivo": "com.kikaoem.vivo.qisiemoji.inputmethod",
            "hack": "com.kikatech.hackkeyboardxposedmodule"
        }
        if s1 in apk:
            for key in apk:
                if key == s1:
                    apkname = apk[key]
                    self.apk_name.append(apkname)
                    # logging.warning('当前apk名称:%s ' % apkname)
                    return self.apk_name
        else:
            try:
                sys.exit()
            except:
                logging.error('apk输入错误，程序终止请重新输入')

    def apk_uninstall(self,s1):
        "卸载apk"
        for i in self.devlists():
            for j in self.apk_dictionary(s1):
                unins1 = r'adb -s %s uninstall %s'%(i,j)
                subprocess.Popen(unins1, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True,
                                          universal_newlines=True)
            #
                unins2 = r'adb -s %s uninstall %s' % (i,"com.kikatech.hackkeyboardxposedmodule")
                subprocess.Popen(unins2, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True,
                                       universal_newlines=True)

                atx = r'adb -s %s uninstall %s' % (i,'com.github.uiautomator')
                subprocess.Popen(atx,stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True,
                                       universal_newlines=True)

                logging.warning('xposed 工具与 {} 卸载完成:{}'.format(j,i))


    def un_layout(self):
        '''
        批量删除操作逻辑
        '''
        for i in self.devlists():
            layout_s = 'adb -s %s shell rm /sdcard/layout_info' % (i) #删除残留layout文件
            res = subprocess.run(layout_s, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True,
                                 universal_newlines=True, )
            if res.returncode == 0:
                logging.warning('删除设备: %s，layout文件，完成' % (i))
            else:
                logging.error('删除失败,失败id: %s' % i)

            wordinput = 'adb -s %s shell rm /sdcard/word_input' % (i)
            res = subprocess.run(wordinput, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True,
                                 universal_newlines=True, )
            if res.returncode == 0:
                logging.warning('删除设备: %s，word_input文件，完成' % (i))

            else:
                logging.error('删除失败,失败id: %s' % i)

            wordout = 'adb -s %s shell rm /sdcard/word_output' % (i)
            res = subprocess.run(wordout, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True,
                                 universal_newlines=True, )
            if res.returncode == 0:
                logging.warning('删除设备: %s，wordout文件，完成' % (i))
            else:
                logging.error('删除失败,失败id: %s' % i)


